/**
 * Created by 陈伟
 * 2017/11/21.
 * 登录所有传参数接口验签
 */
import validator from './validator';
import {md5} from 'vux';

export default {
  signForFormDataOrUrl(datas, isUrl, loginSessionID) {
    if (validator.checkValEmpty(loginSessionID)) {
      return datas;
    }
    let params = {};
    if (datas === null || datas === '') {
      params.jttechSign = md5(loginSessionID);
      return params;
    }
    let keySort = [];
    params = datas;
    if (params) {
      for (let key in params) {
        keySort[keySort.length] = key;
      }
    }
    if (isUrl) {
      keySort.sort();
    }
    // 普通参数的值（非空、非空字符串、非数组）
    let ordinaryParamVal = '';
    for (let index in keySort) {
      for (let param in params) {
        if (param === keySort[index] && params[param] !== null && params[param] !== undefined && !Array.isArray(params[param])) {
          if (params[param] instanceof String) {
            if (params[param].toString().replace(/(^\s*)|(\s*$)/g, '') !== '') {
              ordinaryParamVal += params[param];
            }
          } else {
            ordinaryParamVal += params[param];
          }
        }
      }
    }
    if (!validator.checkValEmpty(loginSessionID)) {
      ordinaryParamVal = md5(ordinaryParamVal + loginSessionID);
    }
    params.jttechSign = ordinaryParamVal;
    return params;
  }
}
